package com.taofuxn.admin.common.util;


import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;

public class SecurityUtil {
    private static final String DES = "DES";

    private SecurityUtil() {
    }

    public static String md5(String encodeStr) {
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

        try {
            byte[] strTemp = encodeStr.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;

            for (int i = 0; i < j; ++i) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 15];
                str[k++] = hexDigits[byte0 & 15];
            }

            return new String(str);
        } catch (Exception var10) {
            return null;
        }
    }

    /**
     * md5加密加盐
     *
     * @param password
     * @param salt
     * @return
     */
    public static final String md5(String password, String salt) {
        return md5(password + salt);
    }

    public static String encodeBase64(String encodeStr) {
        Base64 base64 = new Base64();
        byte[] encodeBytes = base64.encode(encodeStr.getBytes());
        return new String(encodeBytes);
    }

    public static String decodeBase64(String decodeStr) {
        Base64 base64 = new Base64();
        byte[] decodeBytes = base64.decode(decodeStr.getBytes());
        return new String(decodeBytes);
    }

    public static byte[] des(byte[] inputBytes, byte[] keyBytes, int mode) {
        try {
            DESKeySpec desKeySpec = new DESKeySpec(keyBytes);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(mode, secretKey);
            return cipher.doFinal(inputBytes);
        } catch (GeneralSecurityException var7) {
            throw new RuntimeException(var7);
        }
    }

    public static void main(String[] args) {
        System.out.println(md5("admin", "a8061fb4d1041f45"));
    }

}
